package com.zzmcc.stock;

import com.jfinal.plugin.activerecord.Page;
import com.zzmcc.base.BaseController;
import com.zzmcc.jms.model.Stock;
import util.ChristStringUtil;

import java.util.*;

import static com.zzmcc.jms.common.ModelConfigWms.*;

public class StockController extends BaseController {

	public void index() {
		//物料出库
		StringBuffer whee = new StringBuffer();
		List<Object> param = new ArrayList<Object>();

		String materialName = getPara("stock.materialName");
		String projectName = getPara("stock.projectName");
		String homeCounty = getPara("stock.homeCounty");
		String oid = getPara("stock.oid");
		String type = getPara("stock.type");

		if(ChristStringUtil.isNotEmpty(materialName)){
			whee.append(" and c.name like ?");
			param.add("%" + materialName + "%");
		}

		if(ChristStringUtil.isNotEmpty(projectName)){
			whee.append(" and b.name like ?");
			param.add("%" + projectName + "%");
		}

		if(ChristStringUtil.isNotEmpty(homeCounty)){
			whee.append(" and b.homeCounty like ?");
			param.add("%" + homeCounty + "%");
		}

		if(ChristStringUtil.isNotEmpty(oid)){
			whee.append(" and a.oid = ?");
			param.add( oid);
		}

		if(ChristStringUtil.isNotEmpty(type)){
			whee.append(" and a.type = ?");
			param.add( type);
		}


		Page<Stock> pager = Stock.dao.paginate(
				getParaToInt("pageNum", 1),
				getParaToInt("numPerPage", 20),
				"select a.id,a.type,a.oid,a.mid,a.price,a.quantity,a.createDate,a.memo,\n" +
						"b.name projectName,b.projectOwner,c.catalogue,c.name,c.unit,c.model,c.supplier \n",
				" from " + TABLE_Stock +" a left join "+TABLE_Project+" b " +
				" on a.pid=b.pid \n" +
				"left join "+TABLE_Material +" c on a.mid=c.id \n"+
				"where type=-1"+ whee.toString(),
				param.toArray()
		) ;

		setAttr("pager", pager);
		setAttr("materialName", materialName);
		setAttr("projectName", projectName);
		setAttr("homeCounty", homeCounty);
		setAttr("oid", oid);
		setAttr("type", type);
		render("index.html");
	}


}
